package com.cowain.monitor.datasources.config;

import com.cowain.monitor.datasources.DataSourceNames;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 *  
 *  * @author 冬暖
 *  * @projectName pms-monitor
 *  * @title     TransactionConfig   
 *  * @package    com.cowain.monitor.config  
 *  * @description    多事务管理器配置       
 *  * @date   2020/7/28 16:44  
 *  * @version V1.0.0
 *  
 *
 */
@Configuration
public class TransactionConfig {
    public final static String DEFAULT_TX = "defaultTx";
    public final static String SECOND_TX = "secondTx";
    public final static String THIRD_TX = "thirdTx";
    @Bean(name = TransactionConfig.DEFAULT_TX)
    public DataSourceTransactionManager transaction(@Qualifier(DataSourceNames.FIRST) DataSource firstDataSource){
       DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(firstDataSource);
       return dataSourceTransactionManager;
    }
    @Bean(name = TransactionConfig.SECOND_TX)
    public DataSourceTransactionManager secondTransaction(@Qualifier(DataSourceNames.SECOND) DataSource secondDataSource){
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(secondDataSource);
        return dataSourceTransactionManager;
    }
    @Bean(name = TransactionConfig.THIRD_TX)
    public DataSourceTransactionManager thirdTransaction(@Qualifier(DataSourceNames.THIRD) DataSource thirdDataSource){
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(thirdDataSource);
        return dataSourceTransactionManager;
    }
}
